<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="displayModuleTitle"
    @ok="onOkButtonClick"
    ok-text="派单"
  >
    <BasicForm @register="registerForm"></BasicForm>
    <a-alert
      type="info"
      :message="
        planfrequencytype == 0
          ? '本任务为单次/临时任务，请点击下方的派单按钮进行派单。'
          : '如当前时间前已过去的时间段内不需要执行，请设置任务执行的起始时间作为本次派单需要执行的起始时间。如不管任务的执行时间段是否已发生均要执行，请直接点击下方的派单按钮进行派单。'
      "
      show-icon
      style="white-space: pre-wrap"
    />
    <Loading :loading="loading" tip="派单中,请耐心等待" />
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { dispatchTaskSchema } from './taskConsole.data';
  import { dispatchTask } from '/@/api/ehs/task';
  import { Loading } from '/@/components/Loading';
  const { createMessage } = useMessage();
  import moment from 'moment';
  export default defineComponent({
    components: { BasicModal, BasicForm, Loading },
    name: 'IdentifyConfirmModal',
    emits: ['success'],
    setup(props, { emit }) {
      const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = useForm({
        labelWidth: 120,
        schemas: dispatchTaskSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const loading = ref(false);
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      const dataId = ref();
      const planfrequencytype = ref(0);
      const [register, { closeModal }] = useModalInner((data) => {
        resetFields();
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.dataId) {
            dataId.value = unrefData.dataId;
            setFieldsValue({
              plantm: unrefData.plantm,
              tasktitle: unrefData.tasktitle,
            });
          }
          planfrequencytype.value = data.planfrequencytype;
          updateSchema({
            field: 'plantm',
            componentProps: {
              style: 'width:100%',
              disabled: data.planfrequencytype == 0,
              picker:
                data.planfrequencytype == 6
                  ? 'month'
                  : data.planfrequencytype == 7
                  ? 'quarter'
                  : data.planfrequencytype == 8
                  ? 'year'
                  : 'date',
              valueFormat: 'YYYY-MM-DD',
              getPopupContainer: (triggerNode) => document.body,
            },
          });
        }
      });
      async function onOkButtonClick() {
        const value = await validate();
        console.log(value, 'value');
        loading.value = true;
        let tm;
        if (planfrequencytype.value == 6 || planfrequencytype.value == 7) {
          tm = [
            moment(value.plantm[0]).startOf('month').format('YYYY-MM-DD'),
            moment(value.plantm[1]).endOf('month').format('YYYY-MM-DD'),
          ];
        } else if (planfrequencytype.value == 8) {
          tm = [
            moment(value.plantm[0]).startOf('year').format('YYYY-MM-DD'),
            moment(value.plantm[1]).endOf('year').format('YYYY-MM-DD'),
          ];
        } else {
          tm = [
            moment(value.plantm[0]).format('YYYY-MM-DD'),
            moment(value.plantm[1]).format('YYYY-MM-DD'),
          ];
        }
        dispatchTask({
          begintm: tm[0],
          endtm: tm[1],
          id: dataId.value,
        })
          .then((res) => {
            if (!res) {
              createMessage.success('任务派发成功');
              loading.value = false;
              emit('success');
              closeModal();
            } else {
              createMessage.error('任务派发失败');
              loading.value = false;
            }
          })
          .catch((e) => {
            createMessage.error('任务派发失败');
            loading.value = false;
          });
      }

      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      return {
        loading,
        register,
        registerForm,
        closeModal,
        displayModuleTitle,
        onOkButtonClick,
        planfrequencytype,
      };
    },
  });
</script>
